package cn.ml.mybatisdemo.servlet;

import cn.ml.mybatisdemo.dao.StudentMapper;
import cn.ml.mybatisdemo.entity.Student;
import cn.ml.mybatisdemo.entity.StudentExample;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.gson.Gson;
import org.apache.ibatis.session.SqlSession;

import javax.servlet.Servlet;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet(urlPatterns = "/students.do")
public class ListStudentServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //当前页
        String pageNumStr = req.getParameter("page");
        int pageNum = 1;
        if(pageNumStr != null && pageNumStr.matches("\\d+")){ //必须为整数
            pageNum = Integer.parseInt(pageNumStr);
        }
        //每页显示条数
        String pageSizeStr = req.getParameter("rows");
        int pageSize = 3;
        if (pageNumStr != null && pageNumStr.matches("\\d+")){
            pageSize = Integer.parseInt(pageSizeStr);
        }
        //查询条件
        //封装查询条件
        StudentExample example = new StudentExample();
        StudentExample.Criteria criteria = example.createCriteria(); //创建查询条件
        String likeName = req.getParameter("likeName");
        if(likeName != null){
            criteria.andNameLike("%"+likeName+"%");
        }
        String gender = req.getParameter("gender");
        if(gender != null && (gender.equals("男") || gender.equals("女"))){
            criteria.andGenderEqualTo(gender);
        }
        String start = req.getParameter("start");
        String end = req.getParameter("end");
        SimpleDateFormat sdf = new SimpleDateFormat("yyy-MM-dd");
        if(start != null && start.matches("\\d{4}-\\d{2}-\\d{2}")){
            try {
                criteria.andBorndayGreaterThanOrEqualTo(sdf.parse(start));
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        if(end != null && end.matches("\\d{4}-\\d{2}-\\d{2}")){
            try {
                criteria.andBorndayLessThanOrEqualTo(sdf.parse(end));
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        //封装分页信息
        PageHelper.startPage(pageNum,pageSize);
        //获取查询实例
        StudentMapper mapper = ((SqlSession)req.getAttribute("sqlSession")).getMapper(StudentMapper.class);
        List<Student> students = mapper.selectByExample(example);
        PageInfo<Student> pageInfo = new PageInfo<>(students);
        //处理查询结果--产生json-->easyui
        Map map = new HashMap();
        map.put("total",pageInfo.getTotal());
        map.put("rows",students);
        Gson gson = new Gson();
        String json = gson.toJson(map);
        resp.setContentType("application/json;charset=utf-8");
        PrintWriter out = resp.getWriter();
        out.print(json);
        out.flush();
        out.close();


    }
}
